layui.use('table', function(){
    var table = layui.table;
    var i = 1;
    table.render({
        elem: '#demoPhoto'
        ,url:'http://localhost:18088/file/getAllFileByPageByPhoto'
        ,methods: 'get'
        ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
            //,curr: 5 //设定初始在第 5 页
            ,groups: 5 //只显示 1 个连续页码
            ,first: false //不显示首页
            ,last: false //不显示尾页
        }
        ,cols:[
            [
                {field:'id', width:80, title: '序号', sort: true,type:'numbers'}
                ,{field:'fileName', width:400, align: 'center',title: '文件名'}
                ,{field:'fileSize', width:100, title: '文件大小'}
                ,{field:'fileDate', width:120, align: 'center',title: '上传时间'}
                ,{field:'fileType', width:120, align: 'center',title: '文件类型'}
                ,{field:'op', align: 'center',title: '操作',toolbar: '#toolBar'}
            ]
        ]
        ,parseData: function (res) {
            //将原始数据解析成 table 组件所规定的数据
            console.log(res.count);
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.count, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
        ,request: {
            pageName: 'pageNum' // 页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
    });
    table.on('tool(testPhoto)',function (obj) {
        var data = obj.data;
        var e = obj.event;
        var fileUrl;
        if (e == 'download'){
            var $eleForm = $("<form method='post'></form>");
            $eleForm.attr("action","http://localhost:18088/file/download?fileName="+data.fileName);
            $(document.body).append($eleForm);
            $eleForm.submit();
        }else if (e == 'share'){
            axios.post('http://localhost:18088/file/share?fileName='+data.fileName).then(res=>{
                this.fileUrl = res.data.fileUrl;
                console.log("success");
                let transfer = document.createElement('input');
                document.body.appendChild(transfer);
                transfer.value = this.fileUrl;  // 这里表示想要复制的内容
                transfer.select();
                if (document.execCommand('copy')) {
                    document.execCommand('copy');
                }
                transfer.blur();
                console.log('复制成功');
                document.body.removeChild(transfer);
                alert(this.fileUrl+"链接已复制到剪切板");

            })
        }else if (e == 'delete'){
            if (confirm("你确定要删除?")){
                console.log(data.fileName)
                axios.post('http://localhost:18088/file/deleteFile?fileName='+data.fileName).then(res=>{
                    console.log(res);
                    location.href = "main.html";
                })
            }
        }

    })
});
layui.use('element', function(){
    var $ = layui.jquery
        ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    var timer = setInterval(function (){ //进度条方法查看进度
        $.ajax({
            url: 'http://localhost:18088/file/getFileSize',
            success: function (data) {
                //动态设置百分比
                console.log(data.present);
                var percent = data.present;
                $('.layui-progress-bar').attr('lay-percent', percent);
                element.init();
                element.progress('progress', percent)
                if(percent == "100%"){
                    clearInterval(timer); //进度到100%，注意关闭定时器
                }
            },
            error: function (e) {
            }
        });
    });

});
